package com.aya.aop.custom.intro.advisor;

import org.aopalliance.aop.Advice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;

/**
 * 加入此类是为了测试，  多过链路的情况下， 对于引介增强是否有影响。
 * 事实上没有影响， 不管引介增强在链表的哪个位置
 */
@Service
public class IntroBeforeAdvisor implements PointcutAdvisor , Ordered , BeanFactoryAware {
    private static final Logger logger = LoggerFactory.getLogger(IntroBeforeAdvisor.class);

    AspectJExpressionPointcut aspectJExpressionPointcut;
    private BeanFactory beanFactory;

    public IntroBeforeAdvisor() {
       aspectJExpressionPointcut = new AspectJExpressionPointcut();
        aspectJExpressionPointcut.setExpression("execution(* com.aya.aop.custom.intro.point..*(..))");
    }

    @Override
    public Advice getAdvice() {
        return new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] args, Object target) throws Throwable {
                logger.info("before, class:{},method:{}", target.getClass().getName(), method.getName());

            }

        };
    }

    @Override
    public boolean isPerInstance() {
        return true;
    }

    @Override
    public Pointcut getPointcut() {
            return aspectJExpressionPointcut;
    }

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

        this.beanFactory = beanFactory;
        aspectJExpressionPointcut.setBeanFactory(beanFactory);
    }
}
